package com.zkleaner.zookeeper.api;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

// 使用ZkClient创建节点
public class Create_Node_Sample {

    public static void main(String[] args) throws Exception {
//    	ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);
//        //和原生相比 可以递归创建 父节点
//    	String path = "/zk-book/c1";
//        zkClient.createPersistent(path, true);
//        zkClient.deleteRecursive(path);
    	
    	String path = "/zk-book";
        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);
        zkClient.createEphemeral(path, "123");
        
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("Node " + dataPath + " deleted.");
            }
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("Node " + dataPath + " changed, new data: " + data);
            }
        });
        
//        System.out.print(zkClient.readData(path));
        System.out.println("set new date");
        zkClient.writeData(path,"456");
        Thread.sleep(5000);
        System.out.println("set new new date");
        zkClient.writeData(path,"4567");
        Thread.sleep(1000);


        zkClient.delete(path);
    }
}